﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.Windows.Forms.DataVisualization.Charting;

namespace ProcessAnalyzerApplication
{
    public partial class GraphsDialog : Form
    {
        private const int width = 800;
        private const int height = 500;

        private static string GetNameFromPath(string path)
        {
            string[] programPathArray = path.Split(new[] { '\\' });
            return programPathArray[programPathArray.Length - 1];
        }

        public GraphsDialog(Dictionary<string, ArrayList> averagesList, ArrayList applicatoinsNames)
        {
            InitializeComponent();
            this.Size = new Size(width + 4, height + 27);
            GraphsTabControl.Size = new Size(width, height);
            TabPage[] tabs = new TabPage[averagesList.Keys.Count];
            for (int i = 0; i < tabs.Length; i++)
            {
                tabs[i] = new TabPage();
                tabs[i].Size = new Size(width, height);
                tabs[i].Text = averagesList.Keys.ToArray<string>()[i];
                tabs[i].UseVisualStyleBackColor = true;
                tabs[i].Padding = new System.Windows.Forms.Padding(3);
                tabs[i].Location = new System.Drawing.Point(4 + i * 20, 22);
                GraphsTabControl.Controls.Add(tabs[i]);

                ChartArea chartArea = new ChartArea();
                chartArea.Name = "ChartArea";
                Chart chart = new Chart();
                chart.ChartAreas.Add(chartArea);
                chart.Location = new System.Drawing.Point(0, 20);
                chart.Size = new System.Drawing.Size(width - 150, height - 50);
  

                ((System.ComponentModel.ISupportInitialize)(chart)).BeginInit();
                Series[] series = new Series[applicatoinsNames.Count];
                Legend[] legends = new Legend[applicatoinsNames.Count];
                for (int j = 0; j < applicatoinsNames.Count; j++)
                {
                    string applicationName = GetNameFromPath(applicatoinsNames[j].ToString());

                    legends[j] = new Legend();
                    legends[j].Name = applicationName;
                    legends[j].Position = new ElementPosition(97, j * 10, 10, 10);
                    chart.Legends.Add(legends[j]);

                    Label label = new Label();
                    label.AutoSize = true;
                    label.Text = applicationName;
                    label.Location = new Point(width - 140, 35 + (j * 45));
                    label.Size = new Size(20, 20);
                    tabs[i].Controls.Add(label);

                    series[j] = new Series();
                    series[j].ChartArea = "ChartArea";
                    series[j].Name = applicationName;
                    series[j].Legend = applicationName;
                    chart.Series.Add(series[j]);

                    double applicationAverage = Double.Parse(averagesList[tabs[i].Text][j].ToString());
                    chart.Series[applicationName].Points.Add(applicationAverage);
                }

                tabs[i].Controls.Add(chart);
                ((System.ComponentModel.ISupportInitialize)(chart)).EndInit();
            }
        }
    }
}
